home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / imap-3.0 / non-ANSI / c-client / os_ult.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-18  |  16.5 KB  |  618 lines

  1. /*
  2.  * Program:    Operating-system dependent routines -- Ultrix version
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *
  11.  * Date:    11 May 1989
  12.  * Last Edited:    19 May 1993
  13.  *
  14.  * Copyright 1993 by the University of Washington
  15.  *
  16.  *  Permission to use, copy, modify, and distribute this software and its
  17.  * documentation for any purpose and without fee is hereby granted, provided
  18.  * that the above copyright notice appears in all copies and that both the
  19.  * above copyright notice and this permission notice appear in supporting
  20.  * documentation, and that the name of the University of Washington not be
  21.  * used in advertising or publicity pertaining to distribution of the software
  22.  * without specific, written prior permission.  This software is made
  23.  * available "as is", and
  24.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  25.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  26.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  27.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  28.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  29.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  30.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  31.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  32.  *
  33.  */
  34.  
  35. /* TCP input buffer */
  36.  
  37. #define BUFLEN 8192
  38.  
  39.  
  40. /* TCP I/O stream (must be before osdep.h is included) */
  41.  
  42. #define TCPSTREAM struct tcp_stream
  43. TCPSTREAM {
  44.   char *host;            /* host name */
  45.   char *localhost;        /* local host name */
  46.   int tcpsi;            /* input socket */
  47.   int tcpso;            /* output socket */
  48.   int ictr;            /* input counter */
  49.   char *iptr;            /* input pointer */
  50.   char ibuf[BUFLEN];        /* input buffer */
  51. };
  52.  
  53.  
  54. #include "osdep.h"
  55. #include <sys/time.h>
  56. #include <sys/socket.h>
  57. #include <netinet/in.h>
  58. #include <netdb.h>
  59. #include <ctype.h>
  60. #include <errno.h>
  61. extern int errno;        /* just in case */
  62. #include <pwd.h>
  63. #include <syslog.h>
  64. #include "mail.h"
  65. #include "misc.h"
  66.  
  67. /* Write current time in RFC 822 format
  68.  * Accepts: destination string
  69.  */
  70.  
  71. char *days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  72.  
  73. void rfc822_date (date)
  74.     char *date;
  75. {
  76.   int zone;
  77.   char *zonename;
  78.   struct tm *t;
  79.   struct timeval tv;
  80.   struct timezone tz;
  81.   gettimeofday (&tv,&tz);    /* get time and timezone poop */
  82.   t = localtime (&tv.tv_sec);    /* convert to individual items */
  83.   zone = t->tm_gmtoff/60;    /* get timezone from TZ environment stuff */
  84.   zonename = t->tm_zone;
  85.                 /* and output it */
  86.   sprintf (date,"%s, %d %s %d %02d:%02d:%02d %+03d%02d (%s)",
  87.        days[t->tm_wday],t->tm_mday,months[t->tm_mon],t->tm_year+1900,
  88.        t->tm_hour,t->tm_min,t->tm_sec,zone/60,abs (zone) % 60,zonename);
  89. }
  90.  
  91. /* Get a block of free storage
  92.  * Accepts: size of desired block
  93.  * Returns: free storage block
  94.  */
  95.  
  96. void *fs_get (size)
  97.     size_t size;
  98. {
  99.   void *block = malloc (size);
  100.   if (!block) fatal ("Out of free storage");
  101.   return (block);
  102. }
  103.  
  104.  
  105. /* Resize a block of free storage
  106.  * Accepts: ** pointer to current block
  107.  *        new size
  108.  */
  109.  
  110. void fs_resize (block,size)
  111.     void **block;
  112.     size_t size;
  113. {
  114.   if (!(*block = realloc (*block,size))) fatal ("Can't resize free storage");
  115. }
  116.  
  117.  
  118. /* Return a block of free storage
  119.  * Accepts: ** pointer to free storage block
  120.  */
  121.  
  122. void fs_give (block)
  123.     void **block;
  124. {
  125.   free (*block);
  126.   *block = NIL;
  127. }
  128.  
  129.  
  130. /* Report a fatal error
  131.  * Accepts: string to output
  132.  */
  133.  
  134. void fatal (string)
  135.     char *string;
  136. {
  137.   mm_fatal (string);        /* output the string */
  138.   syslog (LOG_ALERT,"IMAP C-Client crash: %s",string);
  139.   abort ();            /* die horribly */
  140. }
  141.  
  142. /* Copy string with CRLF newlines
  143.  * Accepts: destination string
  144.  *        pointer to size of destination string
  145.  *        source string
  146.  *        length of source string
  147.  */
  148.  
  149. char *strcrlfcpy (dst,dstl,src,srcl)
  150.     char **dst;
  151.     unsigned long *dstl;
  152.     char *src;
  153.     unsigned long srcl;
  154. {
  155.   long i,j;
  156.   char *d = src;
  157.                 /* count number of LF's in source string(s) */
  158.   for (i = srcl,j = 0; j < srcl; j++) if (*d++ == '\012') i++;
  159.   if (i > *dstl) {        /* resize if not enough space */
  160.     fs_give ((void **) dst);    /* fs_resize does an unnecessary copy */
  161.     *dst = (char *) fs_get ((*dstl = i) + 1);
  162.   }
  163.   d = *dst;            /* destination string */
  164.                 /* copy strings, inserting CR's before LF's */
  165.   while (srcl--) switch (*src) {
  166.   case '\015':            /* unlikely carriage return */
  167.     *d++ = *src++;        /* copy it and any succeeding linefeed */
  168.     if (srcl && *src == '\012') {
  169.       *d++ = *src++;
  170.       srcl--;
  171.     }
  172.     break;
  173.   case '\012':            /* line feed? */
  174.     *d++ ='\015';        /* yes, prepend a CR, drop into default case */
  175.   default:            /* ordinary chararacter */
  176.     *d++ = *src++;        /* just copy character */
  177.     break;
  178.   }
  179.   *d = '\0';            /* tie off destination */
  180.   return *dst;            /* return destination */
  181. }
  182.  
  183.  
  184. /* Length of string after strcrlfcpy applied
  185.  * Accepts: source string
  186.  *        length of source string
  187.  */
  188.  
  189. unsigned long strcrlflen (s)
  190.     STRING *s;
  191. {
  192.   unsigned long pos = GETPOS (s);
  193.   unsigned long i = SIZE (s);
  194.   unsigned long j = i;
  195.   while (j--) switch (SNX (s)) {/* search for newlines */
  196.   case '\015':            /* unlikely carriage return */
  197.     if (j && (CHR (s) == '\012')) {
  198.       SNX (s);            /* eat the line feed */
  199.       j--;
  200.     }
  201.     break;
  202.   case '\012':            /* line feed? */
  203.     i++;
  204.   default:            /* ordinary chararacter */
  205.     break;
  206.   }
  207.   SETPOS (s,pos);        /* restore old position */
  208.   return i;
  209. }
  210.  
  211. /* Server log in
  212.  * Accepts: user name string
  213.  *        password string
  214.  *        optional place to return home directory
  215.  * Returns: T if password validated, NIL otherwise
  216.  */
  217.  
  218. long server_login (user,pass,home,argc,argv)
  219.     char *user;
  220.     char *pass;
  221.     char **home;
  222.     int argc;
  223.     char *argv[];
  224. {
  225.   struct passwd *pw = getpwnam (lcase (user));
  226.                 /* no entry for this user or root */
  227.   if (!(pw && pw->pw_uid)) return NIL;
  228.                 /* validate password */
  229.   if (strcmp (pw->pw_passwd,(char *) crypt (pass,pw->pw_passwd))) return NIL;
  230.   setgid (pw->pw_gid);        /* all OK, login in as that user */
  231.   initgroups (user,pw->pw_gid);    /* initialize groups */
  232.   setuid (pw->pw_uid);
  233.                 /* note home directory */
  234.   if (home) *home = cpystr (pw->pw_dir);
  235.   return T;
  236. }
  237.  
  238. /* Return my user name
  239.  * Returns: my user name
  240.  */
  241.  
  242. char *uname = NIL;
  243.  
  244. char *myusername ()
  245. {
  246.   return uname ? uname : (uname = cpystr (getpwuid (geteuid ())->pw_name));
  247. }
  248.  
  249.  
  250. /* Return my home directory name
  251.  * Returns: my home directory name
  252.  */
  253.  
  254. char *hdname = NIL;
  255.  
  256. char *myhomedir ()
  257. {
  258.   return hdname ? hdname : (hdname = cpystr (getpwuid (geteuid ())->pw_dir));
  259. }
  260.  
  261.  
  262. /* Build status lock file name
  263.  * Accepts: scratch buffer
  264.  *        file name
  265.  * Returns: name of file to lock
  266.  */
  267.  
  268. char *lockname (tmp,fname)
  269.     char *tmp;
  270.     char *fname;
  271. {
  272.   int i;
  273.   sprintf (tmp,"/tmp/.%s",fname);
  274.   for (i = 6; i < strlen (tmp); ++i) if (tmp[i] == '/') tmp[i] = '\\';
  275.   return tmp;            /* note name for later */
  276. }
  277.  
  278. /* TCP/IP open
  279.  * Accepts: host name
  280.  *        contact port number
  281.  * Returns: TCP/IP stream if success else NIL
  282.  */
  283.  
  284. TCPSTREAM *tcp_open (host,port)
  285.     char *host;
  286.     long port;
  287. {
  288.   TCPSTREAM *stream = NIL;
  289.   int sock;
  290.   char *s;
  291.   struct sockaddr_in sin;
  292.   struct hostent *host_name;
  293.   char hostname[MAILTMPLEN];
  294.   char tmp[MAILTMPLEN];
  295.   /* The domain literal form is used (rather than simply the dotted decimal
  296.      as with other Unix programs) because it has to be a valid "host name"
  297.      in mailsystem terminology. */
  298.                 /* look like domain literal? */
  299.   if (host[0] == '[' && host[(strlen (host))-1] == ']') {
  300.     strcpy (hostname,host+1);    /* yes, copy number part */
  301.     hostname[(strlen (hostname))-1] = '\0';
  302.     if ((sin.sin_addr.s_addr = inet_addr (hostname)) != -1) {
  303.       sin.sin_family = AF_INET;    /* family is always Internet */
  304.       strcpy (hostname,host);    /* hostname is user's argument */
  305.     }
  306.     else {
  307.       sprintf (tmp,"Bad format domain-literal: %.80s",host);
  308.       mm_log (tmp,ERROR);
  309.       return NIL;
  310.     }
  311.   }
  312.  
  313.   else {            /* lookup host name, note that brain-dead Unix
  314.                    requires lowercase! */
  315.     strcpy (hostname,host);    /* in case host is in write-protected memory */
  316.     if ((host_name = gethostbyname (lcase (hostname)))) {
  317.                 /* copy address type */
  318.       sin.sin_family = host_name->h_addrtype;
  319.                 /* copy host name */
  320.       strcpy (hostname,host_name->h_name);
  321.                 /* copy host addresses */
  322.       memcpy (&sin.sin_addr,host_name->h_addr,host_name->h_length);
  323.     }
  324.     else {
  325.       sprintf (tmp,"No such host as %.80s",host);
  326.       mm_log (tmp,ERROR);
  327.       return NIL;
  328.     }
  329.   }
  330.  
  331.                 /* copy port number in network format */
  332.   if (!(sin.sin_port = htons (port))) fatal ("Bad port argument to tcp_open");
  333.                 /* get a TCP stream */
  334.   if ((sock = socket (sin.sin_family,SOCK_STREAM,0)) < 0) {
  335.     sprintf (tmp,"Unable to create TCP socket: %s",strerror (errno));
  336.     mm_log (tmp,ERROR);
  337.     return NIL;
  338.   }
  339.                 /* open connection */
  340.   if (connect (sock,(struct sockaddr *)&sin,sizeof (sin)) < 0) {
  341.     sprintf (tmp,"Can't connect to %.80s,%d: %s",hostname,port,
  342.          strerror (errno));
  343.     mm_log (tmp,ERROR);
  344.     return NIL;
  345.   }
  346.                 /* create TCP/IP stream */
  347.   stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
  348.                 /* copy official host name */
  349.   stream->host = cpystr (hostname);
  350.                 /* get local name */
  351.   gethostname (tmp,MAILTMPLEN-1);
  352.   stream->localhost = cpystr ((host_name = gethostbyname (tmp)) ?
  353.                   host_name->h_name : tmp);
  354.                 /* init sockets */
  355.   stream->tcpsi = stream->tcpso = sock;
  356.   stream->ictr = 0;        /* init input counter */
  357.   return stream;        /* return success */
  358. }
  359.  
  360. /* TCP/IP authenticated open
  361.  * Accepts: host name
  362.  *        service name
  363.  * Returns: TCP/IP stream if success else NIL
  364.  */
  365.  
  366. TCPSTREAM *tcp_aopen (host,service)
  367.     char *host;
  368.     char *service;
  369. {
  370.   TCPSTREAM *stream = NIL;
  371.   struct hostent *host_name;
  372.   char hostname[MAILTMPLEN];
  373.   int i;
  374.   int pipei[2],pipeo[2];
  375.   /* The domain literal form is used (rather than simply the dotted decimal
  376.      as with other Unix programs) because it has to be a valid "host name"
  377.      in mailsystem terminology. */
  378.                 /* look like domain literal? */
  379.   if (host[0] == '[' && host[i = (strlen (host))-1] == ']') {
  380.     strcpy (hostname,host+1);    /* yes, copy without brackets */
  381.     hostname[i-1] = '\0';
  382.   }
  383.                 /* note that Unix requires lowercase! */
  384.   else if (host_name = gethostbyname (lcase (strcpy (hostname,host))))
  385.     strcpy (hostname,host_name->h_name);
  386.                 /* make command pipes */
  387.   if (pipe (pipei) < 0) return NIL;
  388.   if (pipe (pipeo) < 0) {
  389.     close (pipei[0]); close (pipei[1]);
  390.     return NIL;
  391.   }
  392.   if ((i = fork ()) < 0) {    /* make inferior process */
  393.     close (pipei[0]); close (pipei[1]);
  394.     close (pipeo[0]); close (pipeo[1]);
  395.     return NIL;
  396.   }
  397.   if (i) {            /* parent? */
  398.     close (pipei[1]);        /* close child's side of the pipes */
  399.     close (pipeo[0]);
  400.   }
  401.   else {            /* child */
  402.     dup2 (pipei[1],1);        /* parent's input is my output */
  403.     dup2 (pipei[1],2);        /* parent's input is my error output too */
  404.     close (pipei[0]); close (pipei[1]);
  405.     dup2 (pipeo[0],0);        /* parent's output is my input */
  406.     close (pipeo[0]); close (pipeo[1]);
  407.                 /* now run it */
  408.     execl ("/usr/ucb/rsh","rsh",hostname,"exec",service,0);
  409.     _exit (1);            /* spazzed */
  410.   }
  411.  
  412.                 /* create TCP/IP stream */
  413.   stream = (TCPSTREAM *) fs_get (sizeof (TCPSTREAM));
  414.                 /* copy official host name */
  415.   stream->host = cpystr (hostname);
  416.                 /* get local name */
  417.   gethostname (hostname,MAILTMPLEN-1);
  418.   stream->localhost = cpystr ((host_name = gethostbyname (hostname)) ?
  419.                   host_name->h_name : hostname);
  420.   stream->tcpsi = pipei[0];    /* init sockets */
  421.   stream->tcpso = pipeo[1];
  422.   stream->ictr = 0;        /* init input counter */
  423.   return stream;        /* return success */
  424. }
  425.  
  426. /* TCP/IP receive line
  427.  * Accepts: TCP/IP stream
  428.  * Returns: text line string or NIL if failure
  429.  */
  430.  
  431. char *tcp_getline (stream)
  432.     TCPSTREAM *stream;
  433. {
  434.   int n,m;
  435.   char *st,*ret,*stp;
  436.   char c = '\0';
  437.   char d;
  438.                 /* make sure have data */
  439.   if (!tcp_getdata (stream)) return NIL;
  440.   st = stream->iptr;        /* save start of string */
  441.   n = 0;            /* init string count */
  442.   while (stream->ictr--) {    /* look for end of line */
  443.     d = *stream->iptr++;    /* slurp another character */
  444.     if ((c == '\015') && (d == '\012')) {
  445.       ret = (char *) fs_get (n--);
  446.       memcpy (ret,st,n);    /* copy into a free storage string */
  447.       ret[n] = '\0';        /* tie off string with null */
  448.       return ret;
  449.     }
  450.     n++;            /* count another character searched */
  451.     c = d;            /* remember previous character */
  452.   }
  453.                 /* copy partial string from buffer */
  454.   memcpy ((ret = stp = (char *) fs_get (n)),st,n);
  455.                 /* get more data from the net */
  456.   if (!tcp_getdata (stream)) return NIL;
  457.                 /* special case of newline broken by buffer */
  458.   if ((c == '\015') && (*stream->iptr == '\012')) {
  459.     stream->iptr++;        /* eat the line feed */
  460.     stream->ictr--;
  461.     ret[n - 1] = '\0';        /* tie off string with null */
  462.   }
  463.                 /* else recurse to get remainder */
  464.   else if (st = tcp_getline (stream)) {
  465.     ret = (char *) fs_get (n + 1 + (m = strlen (st)));
  466.     memcpy (ret,stp,n);        /* copy first part */
  467.     memcpy (ret + n,st,m);    /* and second part */
  468.     fs_give ((void **) &stp);    /* flush first part */
  469.     fs_give ((void **) &st);    /* flush second part */
  470.     ret[n + m] = '\0';        /* tie off string with null */
  471.   }
  472.   return ret;
  473. }
  474.  
  475. /* TCP/IP receive buffer
  476.  * Accepts: TCP/IP stream
  477.  *        size in bytes
  478.  *        buffer to read into
  479.  * Returns: T if success, NIL otherwise
  480.  */
  481.  
  482. long tcp_getbuffer (stream,size,buffer)
  483.     TCPSTREAM *stream;
  484.     unsigned long size;
  485.     char *buffer;
  486. {
  487.   unsigned long n;
  488.   char *bufptr = buffer;
  489.   while (size > 0) {        /* until request satisfied */
  490.     if (!tcp_getdata (stream)) return NIL;
  491.     n = min (size,stream->ictr);/* number of bytes to transfer */
  492.                 /* do the copy */
  493.     memcpy (bufptr,stream->iptr,n);
  494.     bufptr += n;        /* update pointer */
  495.     stream->iptr +=n;
  496.     size -= n;            /* update # of bytes to do */
  497.     stream->ictr -=n;
  498.   }
  499.   bufptr[0] = '\0';        /* tie off string */
  500.   return T;
  501. }
  502.  
  503.  
  504. /* TCP/IP receive data
  505.  * Accepts: TCP/IP stream
  506.  * Returns: T if success, NIL otherwise
  507.  */
  508.  
  509. long tcp_getdata (stream)
  510.     TCPSTREAM *stream;
  511. {
  512.   fd_set fds;
  513.   FD_ZERO (&fds);        /* initialize selection vector */
  514.   if (stream->tcpsi < 0) return NIL;
  515.   while (stream->ictr < 1) {    /* if nothing in the buffer */
  516.     FD_SET (stream->tcpsi,&fds);/* set bit in selection vector */
  517.                 /* block and read */
  518.     if ((select (stream->tcpsi+1,&fds,0,0,0) < 0) ||
  519.     ((stream->ictr = read (stream->tcpsi,stream->ibuf,BUFLEN)) < 1)) {
  520.       close (stream->tcpsi);    /* nuke the socket */
  521.       if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  522.       stream->tcpsi = stream->tcpso = -1;
  523.       return NIL;
  524.     }
  525.     stream->iptr = stream->ibuf;/* point at TCP buffer */
  526.   }
  527.   return T;
  528. }
  529.  
  530. /* TCP/IP send string as record
  531.  * Accepts: TCP/IP stream
  532.  *        string pointer
  533.  * Returns: T if success else NIL
  534.  */
  535.  
  536. long tcp_soutr (stream,string)
  537.     TCPSTREAM *stream;
  538.     char *string;
  539. {
  540.   return tcp_sout (stream,string,(unsigned long) strlen (string));
  541. }
  542.  
  543.  
  544. /* TCP/IP send string
  545.  * Accepts: TCP/IP stream
  546.  *        string pointer
  547.  *        byte count
  548.  * Returns: T if success else NIL
  549.  */
  550.  
  551. long tcp_sout (stream,string,size)
  552.     TCPSTREAM *stream;
  553.     char *string;
  554.     unsigned long size;
  555. {
  556.   int i;
  557.   fd_set fds;
  558.   FD_ZERO (&fds);        /* initialize selection vector */
  559.   if (stream->tcpso < 0) return NIL;
  560.   while (size > 0) {        /* until request satisfied */
  561.     FD_SET (stream->tcpso,&fds);/* set bit in selection vector */
  562.     if ((select (stream->tcpso+1,0,&fds,0,0) < 0) ||
  563.     ((i = write (stream->tcpso,string,size)) < 0)) {
  564.       puts (strerror (errno));
  565.       close (stream->tcpsi);    /* nuke the socket */
  566.       if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  567.       stream->tcpsi = stream->tcpso = -1;
  568.       return NIL;
  569.     }
  570.     size -= i;            /* count this size */
  571.     string += i;
  572.   }
  573.   return T;            /* all done */
  574. }
  575.  
  576. /* TCP/IP close
  577.  * Accepts: TCP/IP stream
  578.  */
  579.  
  580. void tcp_close (stream)
  581.     TCPSTREAM *stream;
  582. {
  583.  
  584.   if (stream->tcpsi >= 0) {    /* no-op if no socket */
  585.     close (stream->tcpsi);    /* nuke the socket */
  586.     if (stream->tcpsi != stream->tcpso) close (stream->tcpso);
  587.     stream->tcpsi = stream->tcpso = -1;
  588.   }
  589.                 /* flush host names */
  590.   fs_give ((void **) &stream->host);
  591.   fs_give ((void **) &stream->localhost);
  592.   fs_give ((void **) &stream);    /* flush the stream */
  593. }
  594.  
  595.  
  596. /* TCP/IP get host name
  597.  * Accepts: TCP/IP stream
  598.  * Returns: host name for this stream
  599.  */
  600.  
  601. char *tcp_host (stream)
  602.     TCPSTREAM *stream;
  603. {
  604.   return stream->host;        /* return host name */
  605. }
  606.  
  607.  
  608. /* TCP/IP get local host name
  609.  * Accepts: TCP/IP stream
  610.  * Returns: local host name
  611.  */
  612.  
  613. char *tcp_localhost (stream)
  614.     TCPSTREAM *stream;
  615. {
  616.   return stream->localhost;    /* return local host name */
  617. }
  618.